<?php 

include('login_validation.php');
include('functions.php'); 
include('SimpleImage.php'); 

$errors = array();

if (isset($_GET['success']) && $_GET['success'] == "congressdeleted") {
	$success = "The congress was successfully deleted";
}

if (isset($_POST['update']) && $_POST['update'] == 'update') {

	$congress_row_2 = obtenerCongress(getValor('ID_CONGRESS'));

	// login key required
	$loginKeyReq = getValor('LOGIN_KEY_REQUIRED');

	// login key
	$keyPassword = getValor('LOGIN_KEY');
	$keyPasswordConfirm = getValor('CONFIRMATION_KEY');

	if (strlen($keyPassword) != 0 || strlen($keyPasswordConfirm) != 0 || $keyPasswordConfirm == 1) {
			if (strlen($keyPassword) == 0) {
				array_push($errors, "The key password cannot be empty");
			}
			
			if (strlen($keyPasswordConfirm) == 0) {
				array_push($errors, "The confirmation key cannot be empty");
			}
			
			if (strlen($keyPasswordConfirm) > 0 && strlen($keyPassword) > 0 && $keyPasswordConfirm != $keyPassword) {
				array_push($errors, "The confirmation key and the key password must be the same");
			}
	}


	$name = getValor('NAME');

	if (strlen($name) == 0) {
		array_push($errors, "The name cannot be empty");
	} else if (congressNameExistsEdit($name, getValor('ID_CONGRESS'))) {
		array_push($errors, "The name of the congress already exists.");
	}

	$bgColor = "#" . getValor('BACKGROUND_COLOR');
	$primaryColor = "#" . getValor('PRIMARY_COLOR');
	$secondaryColor = "#" . getValor('SECONDARY_COLOR');
	
	$menuButtonColor = "#" . getValor('MENU_BUTTON_COLOR');
	$menuButtonGradientColor = "#" . getValor('MENU_BUTTON_GRADIENT_COLOR');
	
	$rollOverButtonColor = "#" . getValor('ROLLOVER_BUTTON_COLOR');
	$rollOverButtonGradientColor = "#" . getValor('ROLLOVER_BUTTON_GRADIENT_COLOR');

	$forwardEmail = getValor('FORWARD_EMAIL');
	if (strlen($forwardEmail) == 0) {
		array_push($errors, "The forward email cannot be empty");
	}
		
	$twitterLink = getValor('TWITTER_LINK');
	$showTwitterLink = getValor('SHOW_TWITTER_LINK');
	
	if ($showTwitterLink == 1 && strlen($twitterLink) == 0) {
		array_push($errors, "The twitter link cannot be empty");
	}
	
	$facebookLink = getValor('FACEBOOK_LINK');
	$showFacebookLink = getValor('SHOW_FACEBOOK_LINK');

	if ($showFacebookLink == 1 && strlen($facebookLink) == 0) {
		array_push($errors, "The facebook link cannot be empty");
	}

	$websiteLink = getValor('WEBSITE_LINK');
	$showWebSiteLink = getValor('SHOW_WEBSITE_LINK');

	if ($showWebSiteLink == 1 && strlen($websiteLink) == 0) {
		array_push($errors, "The website link cannot be empty");
	}


	// Login Header Image
	$fileLoginHeader = strtolower($_FILES["LOGIN_HEADER"]["name"]);
	$extLoginHeader = strtolower(substr($fileLoginHeader,-3,3));
	$imagenLoginHeader = (date("dmYHis") + 1 ) . "." . $extLoginHeader;
	$targetPathLoginHeader = "images/congress_login/" . $imagenLoginHeader;

	// Login Left Panel Image
	$fileLoginLeftPanel = strtolower($_FILES["LOGIN_LEFT_PANEL"]["name"]);
	$extLoginLeftPanel = strtolower(substr($fileLoginLeftPanel,-3,3));
	$imagenLoginLeftPanel = (date("dmYHis") + 2 ).".".$extLoginLeftPanel;
	$targetPathLeftPanel = "images/congress_login/" . $imagenLoginLeftPanel;

	// Login Footer Image
	$fileLoginFooter = strtolower($_FILES["LOGIN_FOOTER"]["name"]);
	$extLoginFooter = strtolower(substr($fileLoginFooter,-3,3));
	$imagenLoginFooter = (date("dmYHis") + 3 ).".".$extLoginFooter;
	$targetPathFooter = "images/congress_login/" . $imagenLoginFooter;

	// Main Header Image
	$fileMainHeader = strtolower($_FILES["MAIN_HEADER"]["name"]);
	$extMainHeader = strtolower(substr($fileMainHeader,-3,3));
	$imagenMainHeader = (date("dmYHis") + 4 ).".".$extMainHeader;
	$targetPathMainHeader = "images/congress_header/" . $imagenMainHeader;
	
	
	if (sizeof($errors) == 0) {
	
		if ($fileLoginHeader != "") {
			if (move_uploaded_file($_FILES['LOGIN_HEADER']['tmp_name'], $targetPathLoginHeader)) {
				$image = new SimpleImage();
				$image->load($targetPathLoginHeader);
				$ancho = 708;
				$alto = 171;
				$image->resize($ancho, $alto);
				$image->save($targetPathLoginHeader);
			} 
		} else {
			$imagenLoginHeader = $congress_row_2['LOGIN_HEADER'];
		}
		
		if ($fileLoginLeftPanel != "") {
			if (move_uploaded_file($_FILES['LOGIN_LEFT_PANEL']['tmp_name'], $targetPathLeftPanel)) {
				$image = new SimpleImage();
				$image->load($targetPathLeftPanel);
				$ancho = 459;
				$alto = 174;
				$image->resize($ancho, $alto);
				$image->save($targetPathLeftPanel);
			}
		} else {
			$imagenLoginLeftPanel = $congress_row_2['LOGIN_LEFT_PANEL'];
		}
		
		if ($fileLoginFooter != "") {
			if (move_uploaded_file($_FILES['LOGIN_FOOTER']['tmp_name'], $targetPathFooter)) {
				$image = new SimpleImage();
				$image->load($targetPathFooter);
				$ancho = 708;
				$alto = 174;
				$image->resize($ancho, $alto);
				$image->save($targetPathFooter);
			} 
		} else {
			$imagenLoginFooter = $congress_row_2['LOGIN_FOOTER'];
		}
		
		if ($fileMainHeader != "") {
			if (move_uploaded_file($_FILES['MAIN_HEADER']['tmp_name'], $targetPathMainHeader)) {
				$image = new SimpleImage();
				$image->load($targetPathMainHeader);
				$ancho = 880;
				$alto = 149;
				$image->resize($ancho, $alto);
				$image->save($targetPathMainHeader);
			} 
		} else {
			$imagenMainHeader = $congress_row_2['MAIN_HEADER'];
		}	

		$updateSQL = sprintf(
		"UPDATE CONGRESS SET NAME=%s, LOGIN_HEADER=%s, LOGIN_LEFT_PANEL=%s, LOGIN_FOOTER=%s, MAIN_HEADER=%s, BACKGROUND_IMAGE=%s, 
							 BACKGROUND_COLOR=%s, PRIMARY_COLOR=%s, SECONDARY_COLOR=%s, MENU_BUTTON_COLOR=%s, MENU_BUTTON_GRADIENT_COLOR=%s,
							 ROLLOVER_BUTTON_COLOR=%s, ROLLOVER_BUTTON_GRADIENT_COLOR=%s, FORWARD_EMAIL=%s, TWITTER_LINK=%s, SHOW_TWITTER_LINK=%s,
							 FACEBOOK_LINK=%s, SHOW_FACEBOOK_LINK=%s, WEBSITE_LINK=%s, SHOW_WEBSITE_LINK=%s, WEBSITE_TAG=%s, LOGIN_KEY=%s,
							 LOGIN_KEY_REQUIRED=%s WHERE ID_CONGRESS=%s",
				   GetSQLValueString($_POST['NAME'], "text"),
				   GetSQLValueString($imagenLoginHeader, "text"),
				   GetSQLValueString($imagenLoginLeftPanel, "text"),
				   GetSQLValueString($imagenLoginFooter, "text"),
				   GetSQLValueString($imagenMainHeader, "text"),
				   GetSQLValueString("", "text"),
				   GetSQLValueString("#" . $_POST['BACKGROUND_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['PRIMARY_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['SECONDARY_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['MENU_BUTTON_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['MENU_BUTTON_GRADIENT_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['ROLLOVER_BUTTON_COLOR'], "text"),
				   GetSQLValueString("#" . $_POST['ROLLOVER_BUTTON_GRADIENT_COLOR'], "text"),
				   GetSQLValueString($_POST['FORWARD_EMAIL'], "text"),
				   GetSQLValueString("http://" . $_POST['TWITTER_LINK'], "text"),
				   GetSQLValueString($_POST['SHOW_TWITTER_LINK'], "int"),
				   GetSQLValueString("http://" . $_POST['FACEBOOK_LINK'], "text"),
				   GetSQLValueString($_POST['SHOW_FACEBOOK_LINK'], "int"),
				   GetSQLValueString("http://" . $_POST['WEBSITE_LINK'], "text"),
				   GetSQLValueString($_POST['SHOW_WEBSITE_LINK'], "int"),
				   GetSQLValueString("", "text"),
				   GetSQLValueString($_POST['LOGIN_KEY'], "text"),
				   GetSQLValueString($_POST['LOGIN_KEY_REQUIRED'], "int"),
				   GetSQLValueString($_POST['ID_CONGRESS'], "int"));
	 
		executeUpdateQuery($updateSQL);
 
		$success = "The congress has been succefully updated";
 
	}
	
}

$bgcolor = "#229CBA";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

	<title>CONGRESS ADMIN - EDIT CONGRESS</title>

	<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<link href="accordion/style.css" rel="stylesheet" type="text/css" media="screen" />

	<script src="accordion/accordian.pack.js" language="javascript" type="text/javascript">
	</script>

	<style>
		body {
			padding: 0px;
			margin: 0px;
		}
		
		.bodybg {
			background: <?php echo $bgcolor; ?>
			filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='<?php echo $bgcolor; ?>', endColorstr='#FFFFFF');
			background: -webkit-gradient(linear, left top, left bottom, from(<?php echo $bgcolor; ?>), to(#FFFFFF));
			background: -moz-linear-gradient(top,  <?php echo $bgcolor ?>,  #FFFFFF);
			width:100%;
			height: 600px;
		}
	</style>

	<script type="text/javascript" src="jscolor/jscolor.js"></script>

</head>

<body>

	<div align="center" class="bodybg">

		<?php include('admin_header.php'); ?>
	
		<?php include('admin_menu.php'); ?>
	
		<?php include('congress_select.php'); ?>
		
		<?php include('congress_edit_header.php'); ?>
		
		<div id="basic-accordian" style="background-color: white;">

			<?php if (isset($success)) { include('success.php'); } ?>

		    <?php if (getValor('ID_CONGRESS') != "") { ?>		

			<?php if (sizeof($errors) > 0) { include('errors.php'); } ?>

			<?php $congress_row = obtenerCongress(getValor('ID_CONGRESS')); ?>
			
            <form method="post" name="updateCongressForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">

			  <input type="hidden" name="ID_CONGRESS" value="<?php echo getValorReg('ID_CONGRESS', $congress_row); ?>" />
			  <input type="hidden" name="update" value="update" />

              <table align="center" cellpadding="5">

				<tr>
					<td colspan="2">
						<?php 
							$url = $_SERVER['REQUEST_URI'];
							$url = str_replace("congress_edit.php", "", $url);
							$url = "http://" . $_SERVER['HTTP_HOST'] . $url . "congress_login.php?idcongress=" .  getValorReg('ID_CONGRESS', $congress_row);
						?>
						<input type="text" readonly="true" value="<?php echo $url; ?>" style="width: 500px;" />
					</td>
				</tr>

                <tr valign="baseline">
                  <td nowrap align="left">LOGIN KEY REQUIRED:</td>
                  <td>
					<?php $isKeyReq = getValorReg('LOGIN_KEY_REQUIRED', $congress_row); ?>
				  	<input type="radio" value="1" name="LOGIN_KEY_REQUIRED" <?php if ($isKeyReq == '' || $isKeyReq == '1') { echo "checked='checked'"; } ?> 
					/> Yes
					<input type="radio" value="0" name="LOGIN_KEY_REQUIRED" <?php if ($isKeyReq == '0') { echo "checked='checked'"; } ?> 
					/> No
				  </td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">KEY Password</td>
                  <td><input type="text" name="LOGIN_KEY" value="<?php echo getValorReg('LOGIN_KEY', $congress_row); ?>" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">CONFIRMATION KEY</td>
                  <td><input type="text" name="CONFIRMATION_KEY" value="<?php echo getValor('CONFIRMATION_KEY'); ?>" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">NAME</td>
                  <td><input type="text" name="NAME" value="<?php echo getValorReg('NAME', $congress_row); ?>" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed at the top of the login page of the congress">LOGIN HEADER IMAGE (710px x 170px)</td>
                  <td><input type="file" name="LOGIN_HEADER" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed on the left side of the login page of the congress">LOGIN LEFT IMAGE (460px &times; 175px) </td>
                  <td><input type="file" name="LOGIN_LEFT_PANEL" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed at the bottom of the login page of the congress">LOGIN FOOTER IMAGE (710px &times; 175px) </td>
                  <td><input type="file" name="LOGIN_FOOTER" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">MAIN_HEADER</td>
                  <td><input type="file" name="MAIN_HEADER" value="" size="32"></td>
                </tr>
				<?php if (1==2) { ?>
                <tr valign="baseline">
                  <td nowrap align="left">BACKGROUND_IMAGE (WTF)</td>
                  <td><input type="text" name="BACKGROUND_IMAGE" value="" size="32"></td>
                </tr>
				<?php } ?>
				
                <tr valign="baseline">
                  <td nowrap align="left" title="The background color of the congress main page with gradient becoming white">BACKGROUND COLOR</td>
                  <td><input type="text" name="BACKGROUND_COLOR" class="color" value="<?php echo getValorReg('BACKGROUND_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The background color of the menu">PRIMARY COLOR</td>
                  <td><input type="text" name="PRIMARY_COLOR" class="color" value="<?php echo getValorReg('PRIMARY_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The background color of the submenu">SECONDARY COLOR</td>
                  <td><input type="text" name="SECONDARY_COLOR" class="color" value="<?php echo getValorReg('SECONDARY_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The main color of the active button">ACTIVE MENU BUTTON COLOR</td>
                  <td><input type="text" name="MENU_BUTTON_COLOR" class="color" value="<?php echo getValorReg('MENU_BUTTON_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
				
                <tr valign="baseline">
                  <td nowrap align="left" title="The gradient color of the active button">ACTIVE MENU BUTTON GRADIENT COLOR</td>
                  <td><input type="text" name="MENU_BUTTON_GRADIENT_COLOR" class="color" value="<?php echo getValorReg('MENU_BUTTON_GRADIENT_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="left">ROLLOVER_BUTTON_COLOR</td>
                  <td><input type="text" name="ROLLOVER_BUTTON_COLOR" class="color" value="<?php echo getValorReg('ROLLOVER_BUTTON_COLOR', $congress_row); ?>" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="left">ROLLOVER_BUTTON_GRADIENT_COLOR</td>
                  <td>
				  	<input type="text" name="ROLLOVER_BUTTON_GRADIENT_COLOR" class="color" value="<?php echo getValorReg('ROLLOVER_BUTTON_GRADIENT_COLOR', $congress_row); ?>" 
				  		size="32">
				  </td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left">FORWARD EMAIL</td>
                  <td><input type="text" name="FORWARD_EMAIL" value="<?php echo getValorReg('FORWARD_EMAIL', $congress_row); ?>"  size="32"> (i.e. Forgot Password)</td>
                </tr>
				
                <tr valign="baseline">
                  <td nowrap align="left">TWITTER LINK</td>
                  <td>
				    <?php $isTwitterShow = getValorReg('SHOW_TWITTER_LINK', $congress_row); ?>
				  	http:// <input type="text" name="TWITTER_LINK" value="<?php echo str_replace("http://", "", getValorReg('TWITTER_LINK', $congress_row)) ; ?>" size="32">
					<input type="radio" name="SHOW_TWITTER_LINK" value="1" <?php if ($isTwitterShow == '' || $isTwitterShow == '1') { echo "checked='checked'"; } ?>  /> Yes
					<input type="radio" name="SHOW_TWITTER_LINK" value="0" <?php if ($isTwitterShow == '0') { echo "checked='checked'"; } ?>/> No
				  </td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">FACEBOOK LINK</td>
                  <td>
				  	<?php $isFacebookShow = getValorReg('SHOW_FACEBOOK_LINK', $congress_row); ?>
				    http:// <input type="text" name="FACEBOOK_LINK" value="<?php echo str_replace("http://", "", getValorReg('FACEBOOK_LINK', $congress_row)) ; ?>" size="32">
					<input type="radio" name="SHOW_FACEBOOK_LINK" value="1" <?php if ($isFacebookShow == '' || $isFacebookShow == '1') { echo "checked='checked'"; } ?> /> Yes
					<input type="radio" name="SHOW_FACEBOOK_LINK" value="0"  <?php if ($isFacebookShow == '0') { echo "checked='checked'"; } ?> /> No
				  </td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">WEBSITE LINK</td>
                  <td>
					<?php $isWebsiteShow = getValorReg('SHOW_WEBSITE_LINK', $congress_row); ?>
				  	http:// <input type="text" name="WEBSITE_LINK" value="<?php echo str_replace("http://", "", getValorReg('WEBSITE_LINK', $congress_row)) ; ?>" size="32">
				  	<input type="radio" name="SHOW_WEBSITE_LINK" value="1" <?php if ($isWebsiteShow  == '' || $isWebsiteShow  == '1') { echo "checked='checked'"; } ?> /> Yes
					<input type="radio" name="SHOW_WEBSITE_LINK" value="0"  <?php if ($isWebsiteShow  == '0') { echo "checked='checked'"; } ?> /> No
				  </td>
                </tr>


				<?php if (1==2) { ?>
                <tr valign="baseline">
                  <td nowrap align="left">WEBSITE TAG (WTF) </td>
                  <td><input type="text" name="WEBSITE_TAG" value="<?php echo getValorReg('WEBSITE_TAG', $congress_row); ?>" size="32"></td>
                </tr>
				<?php } ?>

                <tr valign="baseline">
                  <td nowrap align="left">&nbsp;</td>
                  <td><input type="submit" value="Save"></td>
                </tr>
				
				<?php if (1==2) { ?>
				<tr>
					<td style="color: red;">WTF: What's this for?</td>
				</tr>
				<?php } ?>
				
				
              </table>

            </form>
			 <?php } ?>	
            <p>&nbsp;</p>
	  </div>
		
	</div>

</body>
</html>
